package projet;

import donnees.ressources.CPU;

/**
 *  This class uses a native C method to periodically collect the cpu load.
 *  It provides the nice, user, kernel, idle and total cpu load values. 
 *  It extends thread so it can run into a separate thread.
 *
 */

public class CpuLoad extends Thread{

    /** time beetween two watches */
    private long watchPeriod;
    private CPU cpu;

    /**
     *  Constructs a cpuLoad object with a link to the specified Architecture 
     *  object.
     *
     *  @param watchPeriod - time beetween two watches
     */
    public CpuLoad(long watchPeriod,CPU cpu){
	this.watchPeriod = watchPeriod;
	this.cpu = cpu;
    }


    /**
     *  Constructs a new Resource object containing the nice, user, kernel and
     *  idle information. This object is then added to the stateInformation 
     *  hashtable in the archi field.
     *
     *  @param nice - the nice value of the cpu load
     *  @param user - the user value of the cpu load
     *  @param kernel - the kernel value of the cpu load
     *  @param idle - the idle value of the cpu load
     */
    public void setValues(float nice, float user, float kernel, float idle) {

	/******* ici vous mettez la gestion de la souvegarde ddes infos dans votre stucture ******/
	/*System.out.println ("nice:"+ nice + ", user:" + user + ", kernel:" +
	kernel + ", idle:" + idle);*/
	cpu.setValeur(nice);
    }

    /**
     *  Native method associated with the C function which collects state 
     *  information.
     */
    public native void StatCpu(int watchPeriod);
//{
    /****** temporaire ********/
//
//	while (true){
//		setValues(1,2,3,4);
//		for (int i=0;i<100000000;i++);//attente
//			
//		 } 
//        // attente
//    }

    /**
     *  Launches a new cpuLoad object in a separate thread
     */
    public void run(){
	int tempo = (int)watchPeriod/1000;
       	this.StatCpu(tempo);
	while(true){ } 
    } 
    
   static {
	System.loadLibrary("stat");
    }
}
